package com.supermap.dataservice.cloud.science.assets.order;

import cn.hutool.core.date.DateUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.supermap.dataservice.mysql.config.DynamicDataSourceContextHolder;
import com.supermap.dataservice.util.JSONUtils;
import com.supermap.dubbo.api.cloud.assets.order.Order;
import com.supermap.dubbo.api.cloud.assets.order.OrderComm;
import com.supermap.dubbo.api.cloud.assets.order.OrderService;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author aqua
 * @description
 * @date 2019年10月22 17:49
 * @email 1301584033@qq.com
 */
@Service
public class OrderServiceImpl implements OrderService {
    @Resource
    private OrderMapper orderMapper;

    @Override
    public JSONUtils getOrderPageList(String userName, String title, Integer state, String bdate, String edate, Integer page, Integer size) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        List<Map<String,Object>> objlist=orderMapper.getOrderPageList(userName,title,state,bdate,edate,page,size);
        objlist.stream().forEach(map ->{  map.put("conmmdit",orderMapper.getOrderComList((Integer)map.get("id")));});
        return new JSONUtils().setCountSum(orderMapper.getOrderPageListCount(userName,title,state,bdate,edate)).setData(objlist);
    }

    @Override
    public JSONUtils manageAudit(Integer orderId,String scontent,String suserId, Integer state) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        String sdata= DateUtil.now();
        orderMapper.manageAudit(orderId,scontent,suserId,state,sdata);
        if(state==3){
            orderMapper.insert(orderMapper.getCommLIst(orderId));
        }
        return new JSONUtils();
    }

    @Override
    public JSONUtils putOrder(Order order) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        orderMapper.putOrder(order);
        Integer ID=orderMapper.getId();
        order.getList().stream().forEach(comm ->{
            comm.setPuid(ID);
            orderMapper.putOrdercomm(comm);
        });
        return new JSONUtils();
    }

    @Override
    public JSONUtils postOrder(OrderComm orderComm) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        orderMapper.postOrderConmm(orderComm);
        return  new JSONUtils();
    }

    @Override
    public JSONUtils delOrder(Integer id) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        orderMapper.del1(id);
        orderMapper.del2(id);
        return  new JSONUtils();
    }
}
